# 学生成绩管理系统 - 简易版
# 不使用函数，适合初学者理解基础语法

# 初始化学生列表
students = []

# 尝试从文件加载数据
try:
    with open('grades.txt', 'r', encoding='utf-8') as f:
        for line in f:
            data = line.strip().split(',')
            if len(data) == 5:
                student = {
                    '学号': data[0],
                    '姓名': data[1],
                    '语文': int(data[2]),
                    '数学': int(data[3]),
                    '英语': int(data[4])
                }
                students.append(student)
    print("数据加载成功！")
except:
    print("未找到数据文件，将创建新文件。")

# 主程序循环
while True:
    print("\n" + "=" * 30)
    print("    学生成绩管理系统")
    print("=" * 30)
    print("1. 添加学生成绩")
    print("2. 查看所有成绩")
    print("3. 删除学生成绩")
    print("4. 修改学生成绩")
    print("5. 查找学生成绩")
    print("6. 成绩统计分析")
    print("7. 退出系统")
    print("=" * 30)

    choice = input("请输入功能序号(1-7): ")

    # 1. 添加学生成绩
    if choice == '1':
        print("\n--- 添加学生成绩 ---")

        # 学号重复检测
        while True:
            student_id = input("请输入学号: ")
            duplicate = False
            for student in students:
                if student['学号'] == student_id:
                    duplicate = True
                    break

            if duplicate:
                print("警告：该学号已存在，请重新输入！")
            else:
                break

        name = input("请输入姓名: ")
        chinese = int(input("请输入语文成绩: "))
        math = int(input("请输入数学成绩: "))
        english = int(input("请输入英语成绩: "))

        new_student = {
            '学号': student_id,
            '姓名': name,
            '语文': chinese,
            '数学': math,
            '英语': english
        }
        students.append(new_student)
        print("添加成功！")

    # 2. 查看所有成绩
    elif choice == '2':
        if len(students) == 0:
            print("系统中暂无学生信息！")
        else:
            print("\n--- 所有学生成绩 ---")
            print("学号\t姓名\t语文\t数学\t英语\t总分\t平均分")
            print("-" * 50)

            for student in students:
                total = student['语文'] + student['数学'] + student['英语']
                average = total / 3
                print(
                    f"{student['学号']}\t{student['姓名']}\t{student['语文']}\t{student['数学']}\t{student['英语']}\t{total}\t{average:.1f}")

    # 3. 删除学生成绩
    elif choice == '3':
        if len(students) == 0:
            print("系统中暂无学生信息！")
        else:
            student_id = input("请输入要删除的学生学号: ")
            found = False

            for i in range(len(students)):
                if students[i]['学号'] == student_id:
                    del students[i]
                    print("删除成功！")
                    found = True
                    break

            if not found:
                print("未找到该学号的学生！")

    # 4. 修改学生成绩 - 选择性修改模式
    elif choice == '4':
        if len(students) == 0:
            print("系统中暂无学生信息！")
        else:
            student_id = input("请输入要修改的学生学号: ")
            found = False

            for student in students:
                if student['学号'] == student_id:
                    found = True
                    print(f"\n当前学生信息：")
                    print(f"学号：{student['学号']}，姓名：{student['姓名']}")
                    print(f"语文：{student['语文']}，数学：{student['数学']}，英语：{student['英语']}")

                    # 修改菜单循环
                    while True:
                        print("\n请选择要修改的项目：")
                        print("1. 修改姓名")
                        print("2. 修改语文成绩")
                        print("3. 修改数学成绩")
                        print("4. 修改英语成绩")
                        print("5. 无修改，返回上一级")

                        mod_choice = input("请输入选项(1-5): ")

                        if mod_choice == '1':
                            new_name = input("请输入新的姓名: ")
                            student['姓名'] = new_name
                            print("姓名修改成功！")
                        elif mod_choice == '2':
                            new_chinese = int(input("请输入新的语文成绩: "))
                            student['语文'] = new_chinese
                            print("语文成绩修改成功！")
                        elif mod_choice == '3':
                            new_math = int(input("请输入新的数学成绩: "))
                            student['数学'] = new_math
                            print("数学成绩修改成功！")
                        elif mod_choice == '4':
                            new_english = int(input("请输入新的英语成绩: "))
                            student['英语'] = new_english
                            print("英语成绩修改成功！")
                        elif mod_choice == '5':
                            break
                        else:
                            print("输入错误，请重新选择！")
                    break

            if not found:
                print("未找到该学号的学生！")

    # 5. 查找学生成绩
    elif choice == '5':
        if len(students) == 0:
            print("系统中暂无学生信息！")
        else:
            student_id = input("请输入要查找的学生学号: ")
            found = False

            for student in students:
                if student['学号'] == student_id:
                    found = True
                    print(f"\n查找到的学生信息：")
                    print(f"学号：{student['学号']}")
                    print(f"姓名：{student['姓名']}")
                    print(f"语文：{student['语文']}")
                    print(f"数学：{student['数学']}")
                    print(f"英语：{student['英语']}")

                    total = student['语文'] + student['数学'] + student['英语']
                    average = total / 3
                    print(f"总分：{total}，平均分：{average:.1f}")
                    break

            if not found:
                print("未找到该学号的学生！")

    # 6. 成绩统计分析
    elif choice == '6':
        if len(students) == 0:
            print("系统中暂无学生信息！")
        else:
            # 收集各科成绩
            chinese_scores = []
            math_scores = []
            english_scores = []

            for student in students:
                chinese_scores.append(student['语文'])
                math_scores.append(student['数学'])
                english_scores.append(student['英语'])

            print("\n--- 成绩统计分析 ---")
            print(
                f"语文 - 平均分：{sum(chinese_scores) / len(chinese_scores):.1f}，最高分：{max(chinese_scores)}，最低分：{min(chinese_scores)}")
            print(
                f"数学 - 平均分：{sum(math_scores) / len(math_scores):.1f}，最高分：{max(math_scores)}，最低分：{min(math_scores)}")
            print(
                f"英语 - 平均分：{sum(english_scores) / len(english_scores):.1f}，最高分：{max(english_scores)}，最低分：{min(english_scores)}")

    # 7. 退出系统
    elif choice == '7':
        # 保存数据到文件
        with open('grades.txt', 'w', encoding='utf-8') as f:
            for student in students:
                line = f"{student['学号']},{student['姓名']},{student['语文']},{student['数学']},{student['英语']}\n"
                f.write(line)
        print("数据保存成功！")
        print("感谢使用学生成绩管理系统！")
        break

    else:
        print("输入错误，请重新输入！")